<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:include page="${pageContext.request.contextPath}/include/pageTitleBread.jsp"/>
<div id="page-content">
    <div class="col-lg-12">
        <div class="panel">
            <div class="panel-heading">
                <h3 class="panel-title">${pageInfo.innerTitle}</h3>
            </div>
            <form id="sysRoleResForm" class="panel-body form-horizontal form-padding">
                <input type="hidden" name="flId" value="${sysRoleRes.flId == null ? '':sysRoleRes.flId}">
                <div class="form-group">
                    <label class="col-md-3 control-label">关联角色</label>
                    <div class="col-md-5">
                        <select name="flRoleId" id="flRoleId" class="form-control">
                            <option>--请选择--</option>
                        </select>
                        <input type="hidden" id="role" value="${sysRoleRes.flRoleId}">
                        <small class="help-block">请选择关联角色</small>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-md-3 control-label">关联资源</label>
                    <div class="col-md-5">
                        <select name="flResId" id="flResId" class="form-control">
                            <option>--请选择--</option>
                        </select>
                        <input type="hidden" id="res" value="${sysRoleRes.flResId}">
                        <small class="help-block">请选择关联角色</small>
                    </div>
                </div>
                <div class="panel-footer">
                    <div class="row">
                        <div class="col-sm-9 col-sm-offset-3">
                            <button class="btn btn-warning" type="reset">重置</button>
                            <button class="btn btn-mint" type="submit">保存</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script>

    function save() {
        var data = common_ajax.ajaxFunc("/sysRoleRes/saveOrUpdate", $('#sysRoleResForm').serialize(), "json", null);
        if (data.code === 0) {
            pointLion.alertMsg("保存成功!", "success", "small", function () {
                doPjax(ctx + '/sysRoleRes');//跳转到列表页
            });
        } else {
            pointLion.alertMsg("操作失败!", "danger", "small", function () {
                doPjax(ctx + '/sysRoleRes');//跳转到列表页
            });
        }
    }

    $(document).ready(function () {
        $.get("${pageContext.request.contextPath}/sysRole/initSelect", function (rst) {
            rst = JSON.parse(rst);
            var roleSelect = $("#flRoleId");
            var roleValue = $("#role").val();
            var selectHtml = "";
            $.each(rst, function (index, obj) {
                if (roleValue === obj.flId) {
                    selectHtml += "<option value='" + obj.flId + "' selected>" + obj.flName + "</option>";
                } else {
                    selectHtml += "<option value='" + obj.flId + "'>" + obj.flName + "</option>";
                }

            });
            roleSelect.html(selectHtml);
        });

        $.get("${pageContext.request.contextPath}/sysRes/initSelect", function (rst) {
            rst = JSON.parse(rst);
            var resSelect = $("#flResId");
            var resValue = $("#res").val();
            var selectHtml = "";
            $.each(rst, function (index, obj) {
                if (resValue === obj.flId) {
                    selectHtml += "<option value='" + obj.flId + "' selected>" + obj.flName + "</option>";
                } else {
                    selectHtml += "<option value='" + obj.flId + "'>" + obj.flName + "</option>";
                }

            });
            resSelect.html(selectHtml);
        });
        $('#sysRoleResForm').bootstrapValidator({
            excluded: [":hidden"],//关键配置，表示只对于隐藏域不进行验证，其他的表单元素都要验证
            fields: {
                "fl_code": {
                    validators: {
                        notEmpty: {
                            message: '*角色代码不能为空!'
                        },
                        stringLength: {
                            min: 2,
                            message: '*角色代码必须大于两位'
                        }
                    }
                },
                "fl_name": {
                    validators: {
                        notEmpty: {
                            message: '*角色名称不能为空'
                        },
                        stringLength: {
                            min: 2,
                            message: '*角色名称必须大于两位'
                        }
                    }
                }
            }
        }).on("success.form.bv", function (e) {
            save();
            return false;//阻止表单跳转
        });
    });
</script>
